using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;
using System.Web;

namespace CodeStory.SmartUpload
{
	public static class SmartService
	{
		static string assemblyGuidString;

		public static string AssemblyGuidString {
			get { return SmartService.assemblyGuidString; }
		}
		static SmartService() {
			GuidAttribute ga = Attribute.GetCustomAttribute(typeof(SmartService).Assembly,typeof(GuidAttribute)) as GuidAttribute;
			if (ga != null) {
				assemblyGuidString = new Guid(ga.Value).ToString("N");
			}
		}

		internal static void InitializeSmartUpload() {
			string temp = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, SmartConfiguration.TempFilePath);
			RegisterFile(Path.Combine(temp, "web.config"), "CodeStory.SmartUpload.Res.web.config");
			//RegisterFile(Path.Combine(temp,"Progress.ashx"),"Luna.SmartUpload.Res.Progress.ashx");
		}

		private static void RegisterFile(string configFileName, string res) {
			try {
				Assembly asm = typeof(SmartUploadHttpModule).Assembly;
				Stream s = asm.GetManifestResourceStream(res);
				FileStream fs = File.Open(configFileName, FileMode.Create);
				byte[] buffer = new byte[s.Length];
				s.Read(buffer, 0, buffer.Length);
				fs.Write(buffer, 0, buffer.Length);
				fs.Flush();
				fs.Close();
				fs.Dispose();
			} catch (FileNotFoundException) {
			} catch (NullReferenceException) {
			} catch {
			}
		}
	}
}
